package teacher;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.ProgressDialog;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

import com.project.institute.DAO.InstituteList;
import com.project.trac.R;

import core.modules.DatabaseConnector;

public class SubmittedList extends InstituteList {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.generic_list);
		setTitle("On time submissions - ");
		
		new AsynchronousTask().execute();
	
	}
	
	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.menu_logout:
	           Logout(this);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	private class AsynchronousTask extends AsyncTask<String, String, String> {

		HashMap<String, String> m;
		ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();

		ProgressDialog progressDialog;
		ListAdapter listAdapter;

		String KEY_USER = "user";
		String KEY_EXPIRATION = "expr";
		
		protected void onPreExecute() {
			super.onPreExecute();
			progressDialog = new ProgressDialog(SubmittedList.this);
			progressDialog.setIcon(android.R.drawable.ic_dialog_info);
			progressDialog.setMessage("please wait...");
			progressDialog.setIndeterminate(true);
			progressDialog.setCancelable(true);
			progressDialog.show();
		}

		protected String doInBackground(String... args) {
			
			try {
				ResultSet rs = DatabaseConnector.getStatement().executeQuery(
						"SELECT b.uid,"
						+ " CONCAT (u.firstname, ' ', u.lastname) AS user,"
						+ " DATEDIFF(a.date_end, b.replyto_date) AS expr"
						+ " FROM forum a, forum b"
						+ " INNER JOIN users u ON u.id = b.uid"
						+ " WHERE b.replyto_id = a.id AND b.title = 'FILE'");

				while (rs.next()) {

					m = new HashMap<String, String>();
					
					if (rs.getInt("expr") >= 0) {
						m.put(KEY_USER, rs.getString("user"));
						m.put(KEY_EXPIRATION, rs.getString("expr") + " days ahead");
						arrayList.add(m);
					}

				}

			} catch (SQLException e) {
				DatabaseConnector.Logger(getClass(), "doInBackground", e);
			} 

			return null;
		}

		protected void onPostExecute(String postExecute) {
			progressDialog.dismiss();
			
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					
					listAdapter = new SimpleAdapter(SubmittedList.this, arrayList,
							R.layout.teacher_forum_submitted_detail, 
							new String[] { KEY_USER, KEY_EXPIRATION }, 
							new int[] {	R.id.tv_person, R.id.tv_ontime});
					setListAdapter(listAdapter);
					
				}

			});

		}

	}

}